﻿using System;
using System.ComponentModel;
using System.IO.Ports;
using System.Linq;
using System.Windows;
using Labthink.PCSoft.UniversalHost4.Model.Setting;
using Labthink.PCSoft.UniversalHost4.Utility;
using SystemSettings;
using Labthink.PCSoft.UniversalHost4.Instrument.Model;
using System.Windows.Media;

namespace Labthink.PCSoft.UniversalHost4.UserControls
{
    public partial class UcSerialPort
    {
        private ComInfo _comInfo { get; set; }

        public string Title { get; set; } = LanguageManager.GetLanguageValue("SerialPortTitle");

        private NotifyManager _notify;

        public string PermissionCode { get; set; } = "Per_SerialPortSetting";

        public UcSerialPort()
        {
            InitializeComponent();
        }


        private void UcSerialPort_OnLoaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
            {
                return;
            }

            var w = Window.GetWindow(this);
            _notify = NotifyManager.InstanceManager(w);

            _comInfo = ConfigAndSettings.GetComInfo();
            string[] ports = SerialPort.GetPortNames();
            if (ports.Any())
            {
                var s = from p in ports
                        select new TextAndValue
                        {
                            Text = p,
                            Value = p
                        };

                CboComPort.Init(s, "Text", "Value");
                var port = _comInfo.PortName;
                CboComPort.CbbChildBox.SelectedIndex = Array.FindIndex(ports, p => p == port);
                TxtBaudRate.Value = _comInfo.BaudRate + "";
                TxtDataBits.Value = _comInfo.DataBits + "";
                TxtParityBit.Value = _comInfo.Parity + "";
                TxtStopBits.Value = _comInfo.StopBits + "";
            }
        }

        private void BtnOk_OnClick(object sender, RoutedEventArgs e)
        {
            if (CboComPort.CbbChildBox.Items.Count > 0 && CboComPort.SelectedIndex >= 0)
            {
                var portName = CboComPort.SelectedValue.ToString();
                _comInfo.PortName = portName;
                bool isOpened = SerialPortHelper.OpenSerialPort(portName);
                if (isOpened)
                {
                    States.Instance.Main.LblSerialPortName.Content = _comInfo.PortName;
                    States.Instance.Main.LblSerialPortStatus.Content = LanguageManager.GetLanguageValue("SerialPortOpened");
                    ConfigAndSettings.SetComInfo(_comInfo);
                    _notify.ShowAlarm(LanguageManager.GetLanguageValue("SerialSetOk"), 3);
                }
                else
                {
                    //States.Instance.Main.LblSerialPortName.Content = Common.ComInfo.PortName;
                    States.Instance.Main.LblSerialPortStatus.Content = LanguageManager.GetLanguageValue("SerialPortClosed");
                    States.Instance.Main.LblSerialPortStatus.Foreground = new SolidColorBrush(Colors.Red);
                    States.Instance.Main.LblSerialPortStatus.Background = new SolidColorBrush(Colors.Yellow);
                    _notify.ShowAlarm(portName+LanguageManager.GetLanguageValue("SerialPortInUse"), 3);
                }
            }
        }
    }
}
